function init(){
	var ouboundProductPriceS=[];
	var deliveryId = GetQueryString("id");// 定义发货单ID
	$("#deliveryId").val(deliveryId);
	$(".select-box ul").on('click','li',function(){
	    $(this).parent().siblings('.show').val($(this).html());
	    $(this).parent().siblings('.hidden').val($(this).attr('data-id'));
	});
	$(".select-box div.fl").click(function(e){
	    e.stopPropagation();
	    var div=$(this);
	    div.children('ul').toggleClass('hide');
	    $(document).click(function() {
	        if (!div.children('ul').hasClass("hide")) {
	            div.children('ul').addClass("hide");
	        }
	    });
	});
	
	/**
	 * 点击确认出库
	 */
	$('.queren').click(ouboundProductPriceS,function(){
		$.ajax({
			type:'post',
			url:'/diconet/warehouse/warehouseProduct/productOutbound',
			data:$('#warehouseForm').serialize(),
			dataType : 'json' ,
			success:function(data){
				if(data.success == 'T'){
					window.location.href = path + "/distributor/delivery_bill";
					$('.add-warehouse').addClass('hide');
				}else{
					layer.msg(data.message, {icon: 5, time: 1000});
				}
			},
			error:function(){
				layer.msg('出库失败', {icon: 5, time: 1000});
			}
		});
	});
	
	/**
	 * 隐藏表单
	 */
	$('.quxiao').click(function(){
	    $('.add-warehouse').addClass('hide');
	});
	
	$('.table-title').on('click','button',function(){
		var num = 0;
		ouboundProductPriceS = [];
		$('.total_count>input').each(function(){
	        var outboundProductPrice={};
        	outboundProductPrice.productPriceId = $(this).parent().attr('data-id');// 商品价格ID
        	outboundProductPrice.productId = $(this).parent().parent().attr('data-id');// 商品ID
        	outboundProductPrice.quantity = $(this).val(); // 出库数量
        	if(num==0)
        		num = $(this).val();
        	ouboundProductPriceS.push(outboundProductPrice);
		});
		if(num == 0){
			layer.msg('请输入出库数量', {icon: 2,time:1000});
			return false;
		}
		
		$('#ouboundProductPriceS').val(JSON.stringify(ouboundProductPriceS));
		
		$('.add-warehouse').removeClass('hide');
	});
	
	/**
	 * 出库数量校验
	 */
	$('.productTable').on('keyup','.total_count>input',function(){
		if(!isPositiveInteger($(this).val())){
			layer.msg('请输入正确的出库数量',{icon: 2,time: 1000});
			$(this).val($(this).val().substr(0,$(this).val().length-1))
		}
	});
	
	/**
	 * 加载仓库选择框
	 */
	$.ajax({
		type: "post",
		url: '/diconet/warehouse/warehouse/list/all',
		dataType: 'json',
		success: function(data){
			var commentContent = '<li>请选择</li>';
			if(data) {
				console.log(data);
				for(var i=0; i<data.length; i++) {
					var comment = data[i];
					var date1 = comment.publishDate == null ? '' : comment.publishDate;
					commentContent += '<li data-id="'+comment.id+'">' + comment.warehouseName+'</li>';
				}
				$('#warehouseSearchBox').html(commentContent);
			}
		},
		error : function(data){
			layer.msg('仓库搜索条件加载失败', {time:500,icon:5});
		}
	});
    loadTitle(deliveryId);
    loadProduct(deliveryId);
}

/**
 * 加载头部信息
 */
function loadTitle(deliveryId){
	$.ajax({
		type:"POST",
		url:"/diconet/delivery/deliveryDetailForTitle",
		data:{deliveryId:deliveryId},
		dataType: 'json',
		success:function(data){
			if(data != null){
				var html = "";
				html += "<p>发货单号：<span>"+data.deliveryCode+"</span></p>";
				html += "<p>客户名称：<span>"+data.purchaserName+"</span></p>";
				html += "<p>订单金额：<span>"+data.orderAmount+"</span></p>";
				var orderState;
				if(data.orderState == 1){
					orderState = '已关闭';
				}else{
					orderState = '处理中';
				}
				var paymentState;
				if(data.paymentState == 1){
					paymentState = '已结清';
				}else{
					paymentState = '付款中';
				}
				html += "<p>订单状态：<span>"+orderState+"</span></p>";
				html += "<p>付款状态：<span>"+paymentState+"</span></p>";
				html += "<p>订单日期：<span>"+data.orderTime+"</span></p>";
				$('.content').find('.order-number').html(html);
			}
		},
		error: function(){
			layer.msg('加载头部信息失败！',{icon:5,time:1000});
		}
	});
}

/**
 * 商品列表
 */
function loadProduct(deliveryId){
	$.ajax({
		type:"POST",
		url:"/diconet/outbound/outboundProduct",
		data:{deliveryId:deliveryId},
		success:function(data){
			if(data != null){
				var html = "";
				var productSpecIds = "";
				for(var i=0; i<data.length; i++){
					var comment = data[i];
					html += "<tr data-id='"+comment.productId+"'>";
					html += "<td><img src='"+comment.imgUrl+"' alt=''/></td>";
					html += "<td><a href='#'>"+comment.productName+"</a></td>";
					html += "<td>";
					var productSpecs = comment.productSpecList;
					for(var j=0; j<productSpecs.length; j++){
						var productSpec = productSpecs[j];
						html += productSpec.specName+":&nbsp;&nbsp;"+productSpec.specValue+";&nbsp;&nbsp;";
						productSpecIds += "," + productSpec.id;
					}
					
					html += "</td>";
					html += "<td>"+comment.deliveryQuantity+"</td>";
					html += "<td>"+comment.outboundQuantity+"</td>";
					html += "<td class='total_count' data-id='"+comment.productPriceId+"'><input data-id='"+productSpecIds.substr(1)+"' type='text' value='0'></td>";
//					html += "<td data-id='"+comment.productPriceId+"'>";
//					if(comment.deliveryQuantity > comment.outboundQuantity){
//						html += "<button data-id='"+productSpecIds.substr(1)+"'>选择仓库</button>";
//					}
//					html += "</td>";
					html += "<td></td></tr>";
				}
				$('.productTable').find('tbody').html(html);
			}
		},
		error: function(){
			layer.msg('加载商品信息失败！',{icon:5,time:1000});
		}
	});
}

/**
 * 获取浏览器地址栏参数
 * @param name
 * @returns
 */
function GetQueryString(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]);
    return null;
}

/**
 * 验证是否为正整数
 * @param s
 * @returns
 */
function isPositiveInteger(s){
	var re = /^(0|[1-9]\d{0,9})$/;
	return re.test(s);
}

init();